/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.pc9.pswing.components;

import br.com.paulocanedo.pc9.PSimpleBorder;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JComponent;
import javax.swing.JPanel;

/**
 *
 * @author paulocanedo
 */
public class PToolBar {

    private JPanel masterComponent = new JPanel(new BorderLayout());
    private JPanel left = new JPanel(new FlowLayout(FlowLayout.LEFT));
    private JPanel right = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    private JPanel center = new JPanel(new FlowLayout(FlowLayout.CENTER));

    public PToolBar() {
        masterComponent.add(left, BorderLayout.WEST);
        masterComponent.add(right, BorderLayout.EAST);
        masterComponent.add(center, BorderLayout.CENTER);

        left.addComponentListener(new AdjustListener());
        right.addComponentListener(new AdjustListener());

        masterComponent.setBorder(new PSimpleBorder(new Insets(1, 6, 1, 6), Color.BLACK, PSimpleBorder.PaintInPosition.BOTTOM));
    }

    private class AdjustListener extends ComponentAdapter {

        @Override
        public void componentResized(ComponentEvent e) {
            adjustCenter();
        }
    }

    private void adjustCenter() {
        int rWidth = right.getPreferredSize().width;
        int lWidth = left.getPreferredSize().width;

        int rHeight = right.getPreferredSize().height;
        int lHeight = left.getPreferredSize().height;

        Dimension newDimension = new Dimension(Math.max(lWidth, rWidth), Math.max(lHeight, rHeight));
        left.setPreferredSize(newDimension);
        right.setPreferredSize(newDimension);
    }

    public void addComponentToLeft(JComponent component) {
        left.add(component);
    }

    public void addComponentToRight(JComponent component) {
        right.add(component);
    }

    public void addComponentToCenter(JComponent component) {
        center.add(component);
    }

    public JComponent getComponent() {
        return masterComponent;
    }

}
